<?php


/**
 * 会员model
 * 
 */

class Member_mdl extends CI_Model
{
	
	//表名
	const TBL_MEMBERS = 'tb_members';
	
	
	
	public function __construct()
	{
		parent::__construct();
	}
	
	/**
	 * 
	 */
	
	public function get_all_member($page = true ,$limit = 0, $offset = 0, $order = 'DESC')
	{
		if($page)
		{
		    if($limit && is_numeric($limit))
	    	{
	    		$this->db->limit(intval($limit));
	    	}
    	
	    	if($offset && is_numeric($offset))
	    	{
	    		$this->db->offset(intval($offset));
	    	}
	    	
//		    if($order && in_array($order, array('DESC', 'ASC')))
//	    	{
//	    		$this->db->orderby(self::TBL_MEMBERS.'.uid', $order);
//	    	}
		}
		
		return $this->db->get(self::TBL_MEMBERS)->result();
	}
	
	/**
     * 获取单个用户信息
     * 
     * @access public
	 * @param int $uid 用户id
     * @return array - 用户信息
     */
	public function get_user_by_id($id)
	{
		$data = array();
		
		$this->db->select('*')->from(self::TBL_MEMBERS)->where('id', $id)->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
		}
		$query->free_result();
		
		return $data;
	}
	
	/**
	 * 获取会员信息
	 * @access public
	 * @param string txnikename
	 * @return array - 会员信息
	 */
	
	public function get_user_by_txnikename($txnikename)
	{
		$data = array();
		$this->db->select('id,txemail,txnikename')->from(self::TBL_MEMBERS)->where('txnikename', $txnikename)->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
		}
		$query->free_result();
		
		return $data;
	}
	
	/**
    * 检查用户是否通过验证
    * 
    * @access public
	* @param string - $name 用户名
	* @param string - $password 密码
    * @return mixed - FALSE/uid
    */	
	public function validate_member($username, $password)
	{
		$data = FALSE;
		
		$this->db->where('tbnikename', $username);
		$query = $this->db->get(self::TBL_MEMBERS);
		
		if($query->num_rows() == 1)
        {
            $data = $query->row_array();
        }
      		
		if(!empty($data))
		{
			$data = (Common::hash_Validate($password, $data['tbpassword'])) ? $data : FALSE;
		}
		
		$query->free_result();
		
		return $data;
	}
	
	/**
	 * add user
	 */
	
	public function add_member($member)
	{
		$this->db->insert(self::TBL_MEMBERS,$member);
		return ($this->db->affected_rows()==1) ? $this->db->insert_id() : FALSE; 
	}
	

	
	/**
    * 修改用户信息
    * 
    * @access public
	* @param int - $uid 用户ID
	* @param int - $data 用户信息
	* @param int - $cipher_password 密码是否hash	
    * @return boolean - success/failure
    */	
	public function update_user($uid, $data, $hashed = TRUE)
	{
		if(!$hashed)
		{
		  $data['password'] = Common::do_hash($data['password']);
		}
		
		$this->db->where('id', intval($uid));
		$this->db->update(self::TBL_MEMBERS, $data);
		
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	
	/**
	 * 密码重置,生成token
	 * @param aray $user - 用户信息(txnikename,id)
	 * @return void
	 */
	
	public function set_reset_password($user)
	{
		$data['token'] = Common::do_hash('reset_'.$user['id'].time());
		$data['uid'] =   $user['id'];
		$data['txemail'] = $user['txemail'];
		$this->db->insert('tb_resetpassword', $data); 
		return ($this->db->affected_rows() > 0) ? $data : FALSE;
	}
	
	/**
	 * 
	 * 取得member数 ...
	 * @return int num
	 */
	
	public function get_member_count()
	{
		$count = 0;
		$count =  $this->db->count_all(self::TBL_MEMBERS);
		return $count;
	}
	
	/**
	 * 
	 * 检查用户名是否存在 ...
	 */
	
	function checkusername($tbnikename)
	{
		$query = $this->db->where('tbnikename',$tbnikename)->get(self::TBL_MEMBERS)->num_rows();
						
		return $query;
		//print_r($query);
		
	}
	
	
}